package cn.laixueit.service;

import cn.laixueit.client.UserClient;
import cn.laixueit.commons.JwtUtils;
import cn.laixueit.commons.UserInfo;
import cn.laixueit.pojo.User;
import cn.laixueit.properties.JwtProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties jwtProperties;

    public String login(String username,String password){

        // 查询，如果存在则生产token，否则返回null

        String token = null;
        try {
            User user = userClient.query(username, password);
            if(user == null){
                return null;
            }
            token = JwtUtils.generateToken(new UserInfo(user.getId(),user.getUsername()), jwtProperties.getPrivateKey(), jwtProperties.getExpire());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }
}
